package com.study.juc.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @program: juc-study
 * @author: jzhou
 * @date: 2022-08-31 11:15
 * @version: 1.0
 * @description:
 **/
public class InheritableThreadLocalDemo implements Runnable {
	private static InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
	private static ExecutorService executorService = Executors.newFixedThreadPool(1);

	public static void main(String[] args) throws Exception{
		System.out.println("----主线程启动");
		inheritableThreadLocal.set("主线程第一次赋值");

		System.out.println("----主线程设置后获取值：" + inheritableThreadLocal.get());
		executorService.submit(new InheritableThreadLocalDemo());
		System.out.println("主线程休眠1秒");
		Thread.sleep(1000);
		inheritableThreadLocal.set("主线程第二次赋值");
		executorService.submit(new InheritableThreadLocalDemo());
		System.out.println("主线程休眠1秒");
		Thread.sleep(1000);
		inheritableThreadLocal.set("主线程第三次赋值");
		new Thread(new InheritableThreadLocalDemo()).start();
		executorService.shutdown();
	}

	@Override
	public void run() {
		System.out.println("----子线程获取值：" + inheritableThreadLocal.get());
	}
}
